package edu.sias.user.service.impl;


import edu.sias.user.constants.Constants;
import edu.sias.user.entity.dto.SessionWebUserDto;
import edu.sias.user.entity.enums.ResponseCodeEnum;
import edu.sias.user.entity.po.UserInfo;
import edu.sias.user.entity.query.UserInfoQuery;
import edu.sias.user.exception.BusinessException;
import edu.sias.user.mapper.UserInfoMapper;
import edu.sias.user.service.EmailCodeService;
import edu.sias.user.service.UserInfoService;
import edu.sias.user.utils.StringTools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 用户信息 业务接口实现
 */
@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {
    @Resource
    private UserInfoMapper<UserInfo, UserInfoQuery> userInfoMapper;
    @Resource
    private EmailCodeService emailCodeService;
    @Override
    public UserInfo getUserInfoByEmail(String email) {
        UserInfo userInfo = userInfoMapper.selectByEmail(email);
        return userInfo;
    }
    /**
     * 注册
     * @param phone 手机号
     * @param password 密码
     */
    @Override
    public UserInfo register1(String phone, String password, String emailCode) {
        UserInfo userInfo1 = userInfoMapper.selectByPhone(phone);
        if (null != userInfo1) {
            return userInfo1;
        }//验证码校验
        emailCodeService.checkCode(phone, emailCode);
        UserInfo userInfo = new UserInfo();
        String username = StringTools.getRandomNumber(Constants.LENGTH_15);
        userInfo.setNickName(username);
        userInfo.setPhone(phone);
        userInfo.setPassword(password);
        userInfo.setJoinTime(new Date());
        userInfo.setStatus(Constants.ONE);

        userInfoMapper.insert(userInfo);
        return userInfo;
    }
    /**
     * 注册
     * @param email
     * @param password
     */
    @Override
    public void register(String email, String password,String emailCode) {
        emailCodeService.checkCode(email, emailCode);
        UserInfo userInfo = new UserInfo();
        String username =  StringTools.getRandomNumber(Constants.LENGTH_15);
        userInfo.setNickName(username);
        userInfo.setEmail(email);
        userInfo.setPassword(password);
        userInfo.setJoinTime(new Date());
        userInfo.setStatus(Constants.ONE);
    }

    @Override
    public UserInfo getUserInfoByPhone(String phone) {
        UserInfo userInfo = userInfoMapper.selectByPhone(phone);
        return userInfo;
    }

    @Override
    public SessionWebUserDto login(String email, String password) {
        return null;
    }
}